package com.edu.atams.common.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 线程池配置
 **/
@Configuration
public class ThreadPoolConfig implements AsyncConfigurer {

    /**
     *
     * @Description: 获得线程池
     */
    @Override
    public Executor getAsyncExecutor() {
        // 线程池
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        // 线程数
        taskExecutor.setCorePoolSize(50);
        // 最大线程数
        taskExecutor.setMaxPoolSize(200);
        // 线程队列最大线程数
        taskExecutor.setQueueCapacity(20000);
        taskExecutor.setKeepAliveSeconds(10);
        // 初始化线程池
        taskExecutor.initialize();
        return taskExecutor;
    }

    /**
     *
     * @Description: 处理异步线程中发生的异常
     */
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        System.out.println("线程异常");
        return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler();
    }
}
